const utils = require("../../../utils/util.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postInfo: null,
    //是否显示隐藏评论
    isHiddenDiscuss: true,
    //是否有点赞或者评论
    isDiscuss: true,
    //是否隐藏遮罩
    isHiddenMask: true,
    //评论输入框是否隐藏
    discussInputHidden: true,
    isShowComment: true,
    commentContent: '',
    currentPostType: 0,
    isShowLikeUser: true,
    inputTxt: '',
    commentInputFocus: false,
    showCommentBtn:false
  },
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      // 来自页面右上角转发按钮
      return {
        title: '快来上车务圈，更多人脉，更多订单！限时免费注册！',
        path: '/pages/index/index',
        imageUrl: '/imgs/share-banner.jpg'
      };
    }
  },
  //评论点击切换
  discussTap: function () {
    let that = this;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animation = animation;
    animation.width(0).step();
    let animationData = animation.export();
    if (that.data.showCommentBtn == true) {
      animation.width("300rpx").step();
      animationData = animation.export();
      that.setData({
        discussInputHidden: true,
        showCommentBtn :false
      });
      setTimeout(function () {
        animation.width("0rpx").step()
        animationData = animation.export();
        that.setData({
          animationData: animationData
        })
      }.bind(that), 200);
    } else {
      animation.width(0).step();
      animationData = animation.export();
      that.setData({
        discussInputHidden: true,
        showCommentBtn: true
      });
      setTimeout(function () {
        animation.width("300rpx").step()
        animationData = animation.export();
        that.setData({
          animationData: animationData
        })
      }.bind(that), 200);
    }
  },
  //底部评论输入框
  discussInputTap: function () {
    let that=this;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animation = animation;
    animation.width(0).step();
    let animationData = animation.export();
    animation.width("300rpx").step();
    animationData = animation.export();
    that.setData({
      discussInputHidden: false,
      commentInputFocus: true
    });
    setTimeout(function () {
      animation.width("0rpx").step()
      animationData = animation.export();
      that.setData({
        animationData: animationData
      })
    }.bind(that), 200);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      currentPostType: options.posttype
    })
    that.getPostInfo(options.postid, options.posttype)
  },
  /**
   * 获取动态信息
   */
  getPostInfo: function (postid, posttype) {
    var that = this
    var data = {
      postId: postid,
      postType: posttype
    }
    // wx.showLoading({
    //   title: '正在查询,请等待..'
    // });
    utils.getData(utils.apiUrl + "/CarCircle/SearchPostById", data, function (res) {
      console.log(res)

      if (res.status == 0) {
        var st = true;
        if (res.data.LikeUser.length == 0 && res.data.CommentInfo.length == 0) {
          st = false;
        }
        that.setData({
          postInfo: res.data,
          isDiscuss: st,
          isShowComment: res.data.CommentInfo.length > 0,
          isShowLikeUser: res.data.LikeUser.length > 0
        })
      } else {
        wx.showToast({
          title: '查询失败',
          icon: 'none'
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none'
      })

    })
    // wx.hideLoading();
  },
  
  /**
   * 点击空白处隐藏评论和点赞
   */
  hiddendiscussBox:function(){
      let that=this;
      
      var animation = wx.createAnimation({
        duration: 150,
        timingFunction: 'linear',
      });
      that.animation = animation;
      animation.width(0).step();
      let animationData = animation.export();
      animation.width("300rpx").step();
      animationData = animation.export();
      that.setData({
        discussInputHidden: true,
        commentInputFocus: false,
        showCommentBtn:false
      });
      setTimeout(function () {
        animation.width("0rpx").step()
        animationData = animation.export();
        that.setData({
          animationData: animationData
        })
      }.bind(that), 200);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      locationCity: app.globalData.defaultCity
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 评论
   */
  bindFormSubmit: function (e) {
    var that = this;
    var formData = e.detail.value;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animation = animation;
    animation.width(0).step();
    let animationData = animation.export();
    animation.width("300rpx").step();
    animationData = animation.export();
    that.setData({
      discussInputHidden: true,
      commentInputFocus: false,
      showCommentBtn: false
    });
    setTimeout(function () {
      animation.width("0rpx").step()
      animationData = animation.export();
      that.setData({
        animationData: animationData
      })
    }.bind(that), 200);
    if (formData.CommentContent == '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }
    var data = {
      OwnerShopID: that.data.postInfo.ShopID,
      PostType: that.data.currentPostType,
      PostId: that.data.postInfo.PostID,
      CommentUserID: utils.getUserId(),
      CommentContent: formData.CommentContent
    }
    // wx.showLoading({
    //   title: '正在提交,请等待..',
    // });
    utils.postData(
      utils.apiUrl + '/Message/AddComment',
      data,
      function (response) {
        var data = response.data;
        var obj = that.data.postInfo;
        if (response.status == 0) {
          obj.CommentInfo.push(data);
          that.setData({
            postInfo: obj,
            isDiscuss: true,
            isShowComment: true,
            discussInputHidden: true,
            isHiddenDiscuss: true,
            inputTxt: ''
          })
        }
        else {
          wx.showModal({
            title: '评论失败',
            content: response.message,
            showCancel: false,
          });
        }
      },
      {},
      function () {
        //   wx.hideLoading();
      }
    )
    return;
  },
  /**
   * 点赞
   */
  addLike: function (e) {
    var that = this;

    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animation = animation;
    animation.width(0).step();
    let animationData = animation.export();
    animation.width("300rpx").step();
    animationData = animation.export();
    that.setData({
      discussInputHidden: true,
      commentInputFocus: false,
      showCommentBtn: false
    });
    setTimeout(function () {
      animation.width("0rpx").step()
      animationData = animation.export();
      that.setData({
        animationData: animationData
      })
    }.bind(that), 200);
    var data = {
      OwnerShopID: that.data.postInfo.ShopID,
      PostType: that.data.currentPostType,
      PostId: that.data.postInfo.PostID,
      CommentUserID: utils.getUserId()
    }
    // wx.showLoading({
    //   title: '正在提交,请等待..',
    // });
    utils.postData(
      utils.apiUrl + '/Message/AddLike',
      data,
      function (response) {
        var data = response.data;
        var obj = that.data.postInfo;
        if (response.status == 0) {
          obj.LikeUser.push(data);
          that.setData({
            postInfo: obj,
            isDiscuss: true,
            // isShowComment: true,
            discussInputHidden: true,
            isHiddenDiscuss: true,
            isShowLikeUser: true
          })
        }
        // else {
        //   wx.showModal({
        //     title: '点赞失败',
        //     content: response.message,
        //     showCancel: false,
        //   }); 
        // }
      },
      {},
      function () {
        //wx.hideLoading();
        that.setData({
          isHiddenDiscuss: true
        })
      }
    )
    return;
  },
  /**   
      * 预览图片  
      */
  previewImage: function (e) {
    var that = this;
    var postIndex = e.target.dataset.postindex;
    var current = that.data.postInfo.PostImgList[postIndex];
    var contentlistTem = that.data.postInfo.PostImgList;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: contentlistTem // 需要预览的图片http链接列表  
    })
  },
  //打电话
  callPhone: function (e) {
    var that = this;
    utils.safeDoAction(function () {
      var oInfo = that.data.postInfo;
      utils.postData(
        utils.apiUrl + "/message/AddPhoneCall",
        {
          OwnerShopID: oInfo.ShopID,
          PostType: oInfo.PostType,
          PostId: oInfo.PostID,
          CommentUserID: utils.getUserId(),
          FromType: 2
        },
        function () {
          wx.makePhoneCall({
            phoneNumber: oInfo.Mobile
          });
        }
      );
    });
  },
  /**
   * 跳转发布动态的店铺
   */
  bindJumpToUserShop: function (e) {
    var shopid = e.currentTarget.dataset.shopid;
    var url = '/pages/carcircle/othermy/othermy?shopid=' + shopid;
    utils.safeOpenUrl(url);
  }
})